<template>
  <div class="app-container">
    <el-card class="table_style">
      <el-form ref="ruleForm" :model="brand" :rules="rules" label-width="100px">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="brand.name" />
        </el-form-item>

        <el-form-item label="品牌logo" prop="logo">
          <el-upload
            class="avatar-uploader"
            :data="qiniuForm"
            :before-upload="beforeUpload"
            :on-success="uploadLogoSuccess"
            action="https://upload.qiniup.com"
          >
            <img v-if="brand.logo" :src="brand.logo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="缩略图" prop="image">
          <el-upload
            class="avatar-uploader"
            :data="qiniuForm"
            :before-upload="beforeUpload"
            :on-success="uploadImageSuccess"
            action="https://upload.qiniup.com"
          >
            <img v-if="brand.image" :src="brand.image" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="展示图" prop="thumb">
          <el-upload
            class="avatar-uploader"
            :data="qiniuForm"
            :before-upload="beforeUpload"
            :on-success="uploadThumbSuccess"
            action="https://upload.qiniup.com"
          >
            <img v-if="brand.thumb" :src="brand.thumb" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="是否启用">
          <el-radio-group v-model="brand.enable" size="medium">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="描述信息" prop="introduce">
          <el-input v-model="brand.introduce" type="textarea" rows="5" />
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置表单</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { createBrand } from '@/api/brand'

const { v4: uuidv4 } = require('uuid')
import { fetchUpload } from '@/api/upload'

export default {
  data() {
    return {
      brand: {
        name: '',
        logo: '',
        image: '',
        thumb: '',
        enable: 0,
        introduce: ''
      },
      qiniuForm: {
        key: '', // 文件名称
        token: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入广告名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '请填写品牌描述信息',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 上传之前
    async beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG或PNG 格式!')
      }

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }

      // 如果验证失败，返回假
      if (isJPG && isLt2M === false) return false

      // 如果验证成功，获取token
      const fileType = file.type === 'image/jpeg' ? 'jpg' : 'png' // 获取扩展名
      const res = await fetchUpload()
      this.qiniuForm = {
        key: `${uuidv4()}.${fileType}`, // 文件名称
        token: res.data.uploadToken
      }
      return true
    },
    // 上传logo图
    uploadLogoSuccess(res) {
      this.brand.logo = 'https://image.holyzq.com/' + res.key
    },
    // 上传缩略图
    uploadImageSuccess(res) {
      this.brand.image = 'https://image.holyzq.com/' + res.key
    },
    // 上传展示图
    uploadThumbSuccess(res) {
      this.brand.thumb = 'https://image.holyzq.com/' + res.key
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        const res = await createBrand(this.brand)
        if (res.code === 20000) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'Brand' })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleChange(value) {
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
